import requests

from ConfigUtil import configClass
import dao.user as userDao

configClass = configClass()
ormMysql = configClass.getItem("urlPath")

go_http_url = ormMysql['go_http_query_url']


# 获取获取好友信息
def get_friend_add_request(dict):
    qqCode = dict['user_id']


def add_friend(json):
    print()
    print(json)
    comment = json['comment']
    if comment != "":
        user = userDao.queryUserByAuth(comment)
        if user is None:
            refused_add_friend(json)
        else:
            agree_add_friend(json)


def refused_add_friend(json):
    set_friend_add_request(json['flag'], False, "")
    pass


def agree_add_friend(json):
    set_friend_add_request(json['flag'], True, "")
    pass


# 发送好友请求
def set_friend_add_request(flag, approve, remark):
    data = {
        "flag": flag,
        "approve": approve,
        "remark": remark
    }
    print(requests.post(go_http_url + "set_friend_add_request", data=data))


def sen_qq_msg(data, user):
    content = data['content']
    if len(content) >= 100:
        content = content[0:97] + '...'
    if user['qqCode'] is None:
        return resultMsg("-1", "未绑定qq号")
    param = {
        "message_type": "private",
        "user_id": user['qqCode'],
        "message": content
    }
    # [CQ:share,url=http://baidu.com,title=百度]
    r = requests.post(go_http_url + "send_msg", data=param)
    param['message'] = '[CQ:share,url=' + data['url'] + ',title=点击查看详情]'
    url = go_http_url + "send_msg"
    requests.post(url, data=param)
    return resultMsg("0", "成功")


def resultMsg(code, msg):
    return str(
        {
            'code': code,
            'msg': msg
        }
    )
